// bitmap：用bit位来记录数据存在与否的一种算法

class MyBitmap{
  constructor(size) {
    this.words = [this.getWordIndex(this.size) + 1]
    this.size = size
  }
  getBit(bitIndex){
    if(bitIndex<0 || bitIndex>this.size-1){
      throw new Error('超过bitmap有效范围')
    }
    let wordIndex = this.getWordIndex(bitIndex)
    return (this.words[wordIndex] & (1 << bitIndex)) != 0
  }
  setBit(bitIndex) {
    if(bitIndex<0 || bitIndex>this.size - 1){
      throw new Error('超过bitmap的有效范围')
    }
    let wordIndex = this.getWordIndex(bitIndex)
    this.words[wordIndex] |= 1 << bitIndex
  }
  getWordIndex(bitIndex) {
    return bitIndex >> 6
  }
}

let bitmap = new MyBitmap(128)
bitmap.setBit(126)
bitmap.setBit(75)
console.log(bitmap.getBit(126))
console.log(bitmap.getBit(78))
